package com.liu.eduService.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.R;
import com.liu.eduService.client.UcenterMemberClient;
import com.liu.eduService.entity.EduComment;
import com.liu.eduService.service.EduCommentService;
import com.liu.entity.UcenterMember;
import com.liu.serviceBase.exception.MyException;
import com.liu.utils.JwtUtils;
import io.jsonwebtoken.Jwt;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author LSJ
 * @since 2022-02-11
 */
@RestController
@RequestMapping("/eduService/comment")
@CrossOrigin
public class EduCommentController {

    @Autowired
    private EduCommentService eduCommentService;
    @Autowired
    private UcenterMemberClient ucenterMemberClient;

    // 分页查询评论
    @GetMapping("getCommentByPage/{page}/{limit}")
    public R getCommentByPage(@PathVariable Long page,
                              @PathVariable Long limit,
                              String courseId ){
        Page<EduComment> commentPage = new Page<>(page, limit);

        Map<String,Object> map = eduCommentService.getCommentByPage(commentPage,courseId);
        return  R.ok().data("map",map);
    }

    @PostMapping("saveComment")
    public  R saveComment(@RequestBody EduComment eduComment, HttpServletRequest request){
        if (ObjectUtils.isEmpty(request)){
            throw new MyException(20001,"saveComment();request为空");
        }
        String id = JwtUtils.getMemberIdByJwtToken(request);
        if (ObjectUtils.isEmpty(id)){
            return R.error().message("用户未登录，请登录");
        }
        eduComment.setMemberId(id);
        UcenterMember member = ucenterMemberClient.getUserInfoById(id);
        System.out.println(member);
        eduComment.setNickname(member.getNickname());
        eduComment.setAvatar(member.getAvatar());
        boolean save = eduCommentService.save(eduComment);
        return save?R.ok().data("eduComment",eduComment):R.error();
    }

// 硬传request不行，失败
//    @PostMapping("saveComment")
//    public  R saveComment(@RequestBody EduComment eduComment, HttpServletRequest request){
//        if (ObjectUtils.isEmpty(request)){
//            throw new MyException(20001,"saveComment();request为空");
//        }
//        String id = JwtUtils.getMemberIdByJwtToken(request);
//        if (ObjectUtils.isEmpty(id)){
//            return R.error().message("用户未登录，请登录");
//        }
//        R info = ucenterMemberClient.getUserInfoByToken(request);
//        System.out.println(info);
//        Map<String, Object> map = info.getData();
//        UcenterMember member = (UcenterMember) map.get("member");
//        System.out.println(member);
//
//        eduComment.setMemberId(id);
////        UcenterMember member = ucenterMemberClient.getUserInfoById(id);
//        eduComment.setNickname(member.getNickname());
//        eduComment.setAvatar(member.getAvatar());
//        boolean save = eduCommentService.save(eduComment);
//        return save?R.ok().data("eduComment",eduComment):R.error();
//    }
}

